export default {
    downloadFromDataURL: function (dataURL: string, filename?: string){
        var blob = this.dataURLToBlob(dataURL);
        var url = window.URL.createObjectURL(blob);

        this.downloadURL(url, filename);
    },
    downloadURL: function (url: string, filename?: string){
        var aTag = document.createElement("a");
        if(!('download' in aTag)){
            console.error('浏览器不支持a标签文件下载！');
        }
        aTag.style.display = "none";
        aTag.href = url;
        if(filename){
            aTag.download = filename;
        }
        console.info('downloadURL: ', url, filename);
        document.body.appendChild(aTag);
        aTag.click();
        document.body.removeChild(aTag);
        window.URL.revokeObjectURL(url);
    },
    downloadDataURL: function (dataURL: string, filename?: string){
        if(!dataURL) return;
        var aTag = document.createElement('a');
        if(!('download' in aTag)){
            console.error('浏览器不支持a标签文件下载！');
        }
        aTag.style.display = 'none';
        if(filename){
            aTag.download = filename;
        }

        aTag.href = dataURL;
        document.body.appendChild(aTag);
        aTag.click();
        document.body.removeChild(aTag);
    },
    downloadStringAsFile: function (stringContent: string, filename?: string, options?: any){
        var aTag = document.createElement('a');
        aTag.style.display = 'none';
        if(filename){
            aTag.download = filename;
        }
        aTag.href = URL.createObjectURL(new Blob([stringContent], options));
        document.body.appendChild(aTag);
        aTag.click();
        document.body.removeChild(aTag);
    },
    dataURLToBlobByType: function (dataURL: string, type: string){
        var _binStr = window.atob(dataURL.split(',')[1]),
            _size = _binStr.length,
            _ary = new Uint8Array(_size);

        for (var i=0; i<_size; i++ ) {
            _ary[i] = _binStr.charCodeAt(i);
        }

        return new Blob([_ary], { type: type || 'image/png' });
    },
    dataURLToBlob: function (dataURL: string) {
        // Code taken from https://github.com/ebidel/filer.js
        var parts = dataURL.split(';base64,');
        var contentType = parts[0].split(":")[1];
        var raw = window.atob(parts[1]);
        var rawLength = raw.length;
        var uInt8Array = new Uint8Array(rawLength);

        for (var i = 0; i < rawLength; ++i) {
            uInt8Array[i] = raw.charCodeAt(i);
        }

        return new Blob([uInt8Array], { type: contentType });
    },
    copyToClipboard: function (value: string, callback: Function){
        var _tempInput = document.createElement('input');
        _tempInput.value = value;
        _tempInput.style.width = '0px !important';
        _tempInput.style.height = '0px !important';
        document.body.appendChild(_tempInput);
        _tempInput.select();
        document.execCommand("Copy");
        document.body.removeChild(_tempInput);
        callback && callback();
	}
};